import csv
import enum
import time
import os


class CrawlerError(enum.Enum):
    catalogGetFail = 'Catalog data get failed'

class LogType(enum.Enum):
    default = 0
    error = 1

def log(content: any, type: LogType):
    current_time = get_time()
    main_path = 'logs'
    check_path(main_path)
    if type is LogType.error:
        err_msg = '\n' + current_time + ' ' + content
        print(err_msg)
        with open(main_path + '/error.log', 'a')as f:
            f.write(err_msg)
    elif type is LogType.default:
        with open(main_path + '/default.log', 'a')as f:
            f.write('\n' + current_time + ' ' + content)
    else:
        with open(main_path + '/ignore.log', 'a')as f:
            f.write('\n' + current_time + ' ' + content)

def save(content: dict, path: str, name: str):
    main_path = 'contents' + '/' + path
    check_path(main_path)
    with open(main_path + '/' + name + '.csv', 'w') as f:
        writer = csv.writer(f)
        for (key, value) in content.items():
            writer.writerow([key, value])

def get_time() -> str:
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

def check_path(path: str):
    if not os.path.exists(path):
        os.mkdir(path)


